Skip to content

perf(v_ahb_diff): replace NOT EXISTS with LEFT JOIN for better query performance#225

Merged
hf-kklein merged 1 commit intomainfrom
perf/left-join-ahb-diff
Dec 16, 2025
Merged

perf(v_ahb_diff): replace NOT EXISTS with LEFT JOIN for better query performance#225
hf-kklein merged 1 commit intomainfrom
perf/left-join-ahb-diff

Conversation

@hf-kklein
Copy link
Copy Markdown
Contributor

…performance

The v_ahb_diff view used NOT EXISTS subqueries to detect added and deleted rows. These correlated subqueries execute once per row, which can be slow especially when SQLite cannot optimize the subquery efficiently.

This change replaces the two NOT EXISTS patterns with LEFT JOIN + IS NULL:

  • Added rows: LEFT JOIN old version, WHERE old.id_path IS NULL
  • Deleted rows: LEFT JOIN new version, WHERE new.id_path IS NULL

LEFT JOIN allows SQLite to use index lookups directly during the join phase rather than running a separate subquery for each candidate row. This is a well-known SQL optimization pattern that typically performs better across database engines.

🤖 Generated with Claude Code

…performance

The v_ahb_diff view used NOT EXISTS subqueries to detect added and
deleted rows. These correlated subqueries execute once per row, which
can be slow especially when SQLite cannot optimize the subquery
efficiently.

This change replaces the two NOT EXISTS patterns with LEFT JOIN + IS NULL:
- Added rows: LEFT JOIN old version, WHERE old.id_path IS NULL
- Deleted rows: LEFT JOIN new version, WHERE new.id_path IS NULL

LEFT JOIN allows SQLite to use index lookups directly during the join
phase rather than running a separate subquery for each candidate row.
This is a well-known SQL optimization pattern that typically performs
better across database engines.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@hf-kklein hf-kklein marked this pull request as ready for review December 16, 2025 11:09
@hf-kklein hf-kklein enabled auto-merge (squash) December 16, 2025 11:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the v_ahb_diff view by replacing correlated NOT EXISTS subqueries with LEFT JOIN + IS NULL patterns for better query performance in SQLite. The view compares AHB versions to detect added, deleted, modified, and unchanged rows.

Key Changes:

  • Replaced NOT EXISTS with LEFT JOIN for detecting added rows (rows in new version but not in old)
  • Replaced NOT EXISTS with LEFT JOIN for detecting deleted rows (rows in old version but not in new)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hf-kklein hf-kklein merged commit 0094509 into main Dec 16, 2025
27 checks passed
@hf-kklein hf-kklein deleted the perf/left-join-ahb-diff branch December 16, 2025 11:45
hf-kklein pushed a commit that referenced this pull request Dec 16, 2025
…r query performance (#225)"

This reverts commit 0094509.
Manual tests show, that it's not faster with the LEFT JOIN approach. At least not for prüfi=13009, FV2510 / FV2604.
hf-kklein added a commit that referenced this pull request Dec 16, 2025
…r query performance (#225)" (#230)

This reverts commit 0094509.
Manual tests show, that it's not faster with the LEFT JOIN approach. At least not for prüfi=13009, FV2510 / FV2604.

Co-authored-by: Konstantin <konstantin.klein+github@hochfrequenz.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants